#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getch.h>

#include "tools.h"
#include "headFunc.h"
#include "initProgram.h"

void headView(void)//显示校长的操作界面
{	
	system("clear");
	printf("  学生：%d人  教师：%d人 \n",STUNUM,TCHNUM);
	puts(" _________________________");
	puts("|                         |");
	puts("|--------欢迎登陆---------|");
	puts("|----输入需要执行的功能---|");
	puts("|-----1.重置本人密码------|");
	puts("|-----2.重置教师密码------|");
	puts("|-----3.添加教师----------|");
	puts("|-----4.删除教师----------|");
	puts("|-----5.修改教师----------|");
	puts("|-----6.显示在职教师------|");
	puts("|-----7.显示离职教师------|");
	puts("|-----8.退出登陆----------|");
	puts("|_________________________|");
}

void headResetHeadPassword(void)//重置校长密码
{
	strcpy(HEADPASSWORD,"admin");
	HEADFLAG = 0;//校长第一次登陆强制修改密码的标志
}

void headResetTchPassword(int num)//重置教师密码
{	
	teacher[num].flag = 0;//教师第一次登陆强制修改密码的标志
	char password[10] = {};
	sprintf(password,"%d",teacher[num].id);
	strcpy(teacher[num].password,password);
	printf("重置密码成功！\n");
}

void headDeleteTch(int tmp)//删除教师
{
	Teacher tch = {};
	teacher[tmp] = tch;
	printf("删除成功\n");
	TCHNUM--;
	anykey_continue();
}

void headViewTchInfo (void)//显示在职教师信息
{
	printf("工号\t姓名\t\t性别\n");
	for(int i=0; i<TCHMAX; i++)
	{
		if(teacher[i].flag != 2 && teacher[i].id)  
		{
			printf("%d\t%-12s\t%s\n",teacher[i].id,teacher[i].name,teacher[i].sex=='w'?"女":"男");
		}
	}
	printf("以上均为在职教师信息!\n");
	anykey_continue();
}

void headViewLeaveTchInfo (void)//显示离职教师信息
{
	printf("工号\t姓名\t\t性别\n");
	int ifExist = 0;//判断是否有离职教师
	for(int i=0; i<TCHMAX; i++)
	{
		
		if(2 == teacher[i].flag)
		{
			ifExist++;
			printf("%d\t%-12s\t%s\n",teacher[i].id,teacher[i].name,teacher[i].sex=='w'?"女":"男");
		}
	}
	if(0 == ifExist)
	{
		printf("暂无离职教师信息！\n");
		
	}
	else
	{
		printf("以上均为离职教师信息！\n");
	}
	anykey_continue();
}

void headModifyTchView(void)
{
	printf("1、修改教师基本信息\n");
	printf("2、修改教师帐号状态\n");
	printf("请选择功能: \n");	
	switch(getch())
	{
		case '1': 
		{	
		    int tchId = 0;
		    printf("请输入教师工号：\n");
		    scanf("%d",&tchId);
		    while(tchId < 101 || tchId > 200 || 0 == teacher[tchId-101].id)
		    {
		        printf("工号有误或不合法，请重新输入:\n");
		        scanf("%d",&tchId);
		    }
		    int num = tchId - 101;
		    headModifyTchBasicInfo(num);
		    anykey_continue();
		}	break;
		case '2':
		{
		    int tchId = 0;
		    int newFlag = 0;
		    printf("请输入教师工号：\n");
		    scanf("%d",&tchId);
		    while(tchId < 101 || tchId > 200 || 0 == teacher[tchId-101].id)
		    {
		        printf("工号有误或不合法，请重新输入:\n");
		        scanf("%d",&tchId);
		    }
		    system("clear");
		    printf("工号\t帐号状态\n");
		    printf("%d\t%s\n",teacher[tchId-101].id,teacher[tchId-101].flag==0?"初始化":teacher[tchId-101].flag==1?"正常":teacher[tchId-101].flag==2?"已离职":"已锁定");
		    printf("请输入新的帐号状态(1:正常,2:离职,-1:锁定中)\n");
		    scanf("%d",&newFlag);
		    while(teacher[tchId-101].flag == newFlag || -1 > newFlag || 2 < newFlag || 0 == newFlag)
		    {
		        printf("输入有误或与原状态重复，请重新输入:\n");
		        scanf("%d",&newFlag);
		    }
		    teacher[tchId-101].flag = newFlag;
		    printf("修改成功！\n");
		    anykey_continue();
		}	break;
	}
}

void headModifyTchBasicInfo(int num)
{
	printf("工号\t姓名\t\t性别\n");
	printf("%d\t%-12s\t%s\n", teacher[num].id, teacher[num].name, teacher[num].sex=='w'?"女":"男");
	printf("请输入要修改成的姓名、性别\n");
	scanf("%s %c",teacher[num].name,&teacher[num].sex);
	printf("修改成功！\n");
}

